package bean;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Operacoes {

	private StringBuilder condition;
	private List<MetodosPEWSXML> metodos;

	/**
	 * @param args
	 */

	
	public Operacoes() {
		this.condition = new StringBuilder();
		this.metodos = new ArrayList<MetodosPEWSXML>(); 
	}

	public void clearCondition(){
		this.condition = new StringBuilder();
	}

	public String getCondition(){
		return this.condition.toString();
	}
	
	public String getMedotodName(String alias){
		for (MetodosPEWSXML m : this.metodos) {
			if (m.getAlias().equalsIgnoreCase(alias));
				return m.getMetodo();
		}
		return null;
	}
	
	public List<MetodosPEWSXML> getMetodos() {
		return metodos;
	}



	public void setMetodos(List<MetodosPEWSXML> metodos) {
		this.metodos = metodos;
	}



	public void or(NodeList childNodes) {
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				logicos(childNodes.item(i));
				if(i<childNodes.getLength()-2){
					this.condition.append(" or ");
				}
			}		
		}
		
	}

	public void and(NodeList childNodes) {
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				logicos(childNodes.item(i));
				if(i<childNodes.getLength()-2){
					this.condition.append(" and ");
				}
			}		
		}

	}
	
	public void logicos(Node node) {
		String nome = node.getNodeName();

		if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
			aritmeticos(node);
		} else if (node.getNodeName().equalsIgnoreCase("gt")){
			gt(node);			
		} else if (node.getNodeName().equalsIgnoreCase("lt")){
			lt(node);
		} else if (node.getNodeName().equalsIgnoreCase("leq")){
			leq(node);			
		} else if (node.getNodeName().equalsIgnoreCase("geq")){
			geq(node);
		} else if (node.getNodeName().equalsIgnoreCase("eq")){
			eq(node);			
		} else if (node.getNodeName().equalsIgnoreCase("neq")){
			neq(node);
		} else {
			System.out.println("OPERADOR LOGICO INEXISTENTE");
		}
	}



	public void gt(Node node){
		NodeList childNodes = node.getChildNodes();
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				String nome = childNodes.item(i).getNodeName();
				if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
					aritmeticos(childNodes.item(i));
				}  else {					
					if(childNodes.item(i).getNodeName().equalsIgnoreCase("minusUn")){
						this.condition.append("-" + childNodes.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
					} else if (childNodes.item(i).getNodeName().equalsIgnoreCase("pewsCounter")){
						String counter = getMedotodName(childNodes.item(i).getAttributes().item(2).getNodeValue()) + "." + childNodes.item(i).getAttributes().item(1).getNodeValue() + childNodes.item(i).getAttributes().item(0).getNodeValue();
						this.condition.append(counter);
					} else{
						this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
					}					
					
					if(i<childNodes.getLength()-2){
						this.condition.append(" > ");
					}
				}
			}		
		}	
	}
	
	public void lt(Node node) {
		NodeList childNodes = node.getChildNodes();
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				String nome = childNodes.item(i).getNodeName();
				if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
					aritmeticos(childNodes.item(i));
				} else {					
					if(childNodes.item(i).getNodeName().equalsIgnoreCase("minusUn")){
						this.condition.append("-" + childNodes.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
					} else if (childNodes.item(i).getNodeName().equalsIgnoreCase("pewsCounter")){
						String counter = getMedotodName(childNodes.item(i).getAttributes().item(2).getNodeValue()) + "." + childNodes.item(i).getAttributes().item(1).getNodeValue() + childNodes.item(i).getAttributes().item(0).getNodeValue();
						this.condition.append(counter);
					}else{
						this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
					}					
					
					if(i<childNodes.getLength()-2){
						this.condition.append(" < ");
					}
				}
			}		
		}		
	}

	public void leq(Node node){
		NodeList childNodes = node.getChildNodes();
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				String nome = childNodes.item(i).getNodeName();
				if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
					aritmeticos(childNodes.item(i));
				} else {					
					if(childNodes.item(i).getNodeName().equalsIgnoreCase("minusUn")){
						this.condition.append("-" + childNodes.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
					} else if (childNodes.item(i).getNodeName().equalsIgnoreCase("pewsCounter")){
						String counter = getMedotodName(childNodes.item(i).getAttributes().item(2).getNodeValue()) + "." + childNodes.item(i).getAttributes().item(1).getNodeValue() + childNodes.item(i).getAttributes().item(0).getNodeValue();
						this.condition.append(counter);
					} else{
						this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
					}					
					
					if(i<childNodes.getLength()-2){
						this.condition.append(" <= ");
					}
				}
			}		
		}
	}
	
	public void geq(Node node){
		NodeList childNodes = node.getChildNodes();
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				String nome = childNodes.item(i).getNodeName();
				if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
					aritmeticos(childNodes.item(i));
				} else {					
					if(childNodes.item(i).getNodeName().equalsIgnoreCase("minusUn")){
						this.condition.append("-" + childNodes.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
					} else if (childNodes.item(i).getNodeName().equalsIgnoreCase("pewsCounter")){
						String counter = getMedotodName(childNodes.item(i).getAttributes().item(2).getNodeValue()) + "." + childNodes.item(i).getAttributes().item(1).getNodeValue() + childNodes.item(i).getAttributes().item(0).getNodeValue();
						this.condition.append(counter);
					} else{
						this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
					}					
					
					if(i<childNodes.getLength()-2){
						this.condition.append(" >= ");
					}
				}
			}		
		}
	}
	

	public void eq(Node node) {
		NodeList childNodes = node.getChildNodes();
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				String nome = childNodes.item(i).getNodeName();
				if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
					aritmeticos(childNodes.item(i));
				} else {					
					if(childNodes.item(i).getNodeName().equalsIgnoreCase("minusUn")){
						this.condition.append("-" + childNodes.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
					} else if (childNodes.item(i).getNodeName().equalsIgnoreCase("pewsCounter")){
						String counter = getMedotodName(childNodes.item(i).getAttributes().item(2).getNodeValue()) + "." + childNodes.item(i).getAttributes().item(1).getNodeValue() + childNodes.item(i).getAttributes().item(0).getNodeValue();
						this.condition.append(counter);
					} else{
						this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
					}					
					
					if(i<childNodes.getLength()-2){
						this.condition.append(" == ");
					}
				}
			}		
		}
	}
	
	public void neq(Node node){
		NodeList childNodes = node.getChildNodes();
		for(int i=0; i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				String nome = childNodes.item(i).getNodeName();
				if(nome.equalsIgnoreCase("sum") || nome.equalsIgnoreCase("minus") || nome.equalsIgnoreCase("mult") || nome.equalsIgnoreCase("div")){
					aritmeticos(childNodes.item(i));
				} else {					
					if(childNodes.item(i).getNodeName().equalsIgnoreCase("minusUn")){
						this.condition.append("-" + childNodes.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
					} else if (childNodes.item(i).getNodeName().equalsIgnoreCase("pewsCounter")){
						String counter = getMedotodName(childNodes.item(i).getAttributes().item(2).getNodeValue()) + "." + childNodes.item(i).getAttributes().item(1).getNodeValue() + childNodes.item(i).getAttributes().item(0).getNodeValue();
						this.condition.append(counter);
					} else{
						this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
					}					
					
					if(i<childNodes.getLength()-2){
						this.condition.append(" != ");
					}
				}
			}		
		}
	}
	
	public void aritmeticos(Node node) {
		NodeList childNodes = node.getChildNodes();
		if(node.getNodeName().equalsIgnoreCase("sum")){
			sum(childNodes);			
		} else if(node.getNodeName().equalsIgnoreCase("minus")){
			minus(childNodes);
		} else if(node.getNodeName().equalsIgnoreCase("mult")){
			mult(childNodes);
		} else if(node.getNodeName().equalsIgnoreCase("div")){
			div(childNodes);
		} else {
			System.out.println("OPERADOR ARITMETICO INEXISTENTE");
		}
		
	}

	public void div(NodeList childNodes) {
		for(int i=0;i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
				if(i<childNodes.getLength()-2){
					this.condition.append(" / ");
				}
			}
		}
		
	}

	public void mult(NodeList childNodes) {
		for(int i=0;i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
				if(i<childNodes.getLength()-2){
					this.condition.append(" * ");
				}
			}
		}
		
	}

	public void minus(NodeList childNodes) {
		for(int i=0;i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
				if(i<childNodes.getLength()-2){
					this.condition.append(" - ");
				}
			}
		}
		
	}

	public void sum(NodeList childNodes) {
		for(int i=0;i<childNodes.getLength();i++){
			if(childNodes.item(i).getNodeType() == Node.ELEMENT_NODE && childNodes.item(i).getNodeType() != Node.TEXT_NODE){
				this.condition.append(childNodes.item(i).getAttributes().item(0).getNodeValue());
				if(i<childNodes.getLength()-2){
					this.condition.append(" + ");
				}
			}
		}		
	}

}
